$(document).ready(function() {
    $('#icon_upload').uploadify({
        'uploader'  : '/js/adminbase/uploadify/uploadify.swf',
        'script'    : '/js/adminbase/uploadify/uploadify.php',
        'cancelImg' : '/js/adminbase/uploadify/cancel.png',
        'folder'    : '/images/adminbase/image',
     	'onComplete'  : function(event, ID, fileObj, response, data) {  
    	  	showimageicon("img_link",response);
      		},
        'auto'      : true
    });
    
    $('#banner_upload').uploadify({
        'uploader'  : '/js/adminbase/uploadify/uploadify.swf',
        'script'    : '/js/adminbase/uploadify/uploadify.php',
        'cancelImg' : '/js/adminbase/uploadify/cancel.png',
        'folder'    : '/images/adminbase/banner',
     	'onComplete'  : function(event, ID, fileObj, response, data) {  
    	  	showimageicon("banner_link",response);
      		},
        'auto'      : true
    });
    
    $('#file_upload').uploadify({
        'uploader'  : '/js/adminbase/uploadify/uploadify.swf',
        'script'    : '/js/adminbase/uploadify/uploadify.php',
        'cancelImg' : '/js/adminbase/uploadify/cancel.png',
        'folder'    : '/file',
     	'onComplete'  : function(event, ID, fileObj, response, data) {   
    	  	showfilename("link_download", response, "file_size",fileObj.size);
      		},
        'auto'      : true
    });
    $(".close").click(function(){
        $(this).parent().remove();
    });
    $('.numeric').numeric();
});

function showimage(name_id,image_link){
	var nameold = jQuery("#"+name_id).val();
	var count_images = jQuery("#count_images").val();
	var namenew;
	if(nameold) namenew = nameold + "||" + image_link;
	else namenew = image_link;
	jQuery("#"+name_id).val(namenew);
	var html = "<span id='images_view"+count_images+"'><img src='"+image_link+"' width='100' height ='100'/><img src='/images/adminbase/delete.png' width='20' height ='20' onclick=delete_images('images_view"+count_images+"','"+image_link+"') /></span>";
	jQuery("#count_images").val(count_images + 1);
	jQuery("#add_images").append(html);
}

function showimageicon(name_id,image_link){
	jQuery("#"+name_id).val(image_link);
	var image = "<img src='"+image_link+"' />";
	jQuery("#show_"+name_id).html(image);
}

function showfilename(name_id,file_link, file_size, size){
    jQuery("#"+name_id).css("display", "block");
	jQuery("#"+name_id).val(file_link);
    jQuery("#"+file_size).css("display", "block");
    m_size = Math.round(size/1024);
	jQuery("#"+file_size).val(m_size+" KB");
}

function search(){
	var key=jQuery("#search").val().trim();
	var type=jQuery("#type").val();
	if (key){
		jQuery.ajax({
			type:"POST",
			url:"/admin/index/search",
			data:"key="+key+"&type="+type,
			success:function(html){
				jQuery("#list_product").html(html);
		}
		})
	}
}

function edit(id){
	window.location = "/admin/index/editproduct?id="+id;
}

function del(id){
	window.location = "/admin/index/delete?id="+id;
}

function showbanner(banner_link){
	var image = "<img src='"+banner_link+"' height=200px />"
	jQuery("#show_banner").append(image);
	jQuery("#banner").val(banner_link);
}

function delbanner(id){
	jQuery("#banner_"+id).hide();
	jQuery.ajax({
		type:"POST",
		url:"/admin/index/delbanner",
		data:"id="+id	
	});
}

function addsite(){
	var site = jQuery("#add_site").val();
	jQuery.ajax({
		type:"POST",
		url:"/admin/index/addsite",
		data:"site="+site,
		success:function(html){
			jQuery("#site_table").append(html);
	}
	})
}

function showFormAdd(){
	$("#formadd").show();
}
function showFormEdit(id){
//	alert(id);
	$("#formadd").hide();
	$("#formedit").show();
	$("#temp").val(id);
}

function edit_acount(id){
	
	url = '/admin/admin/editacount?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function editsite(id){
	
	url = '/admin/index/editsite?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function editcampaign(id){
	
	url = '/admin/admin/editcampaign?id='+id;
	$("#formnew").load(url);
	
	$("#formnew").fadeIn();
	$("#formadd").remove();
}

function delete_acount(id){
//	alert(id);
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	  type:"POST",
					  url: "/admin/admin/deleteacount",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function delete_site(id){
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	  type:"POST",
					  url: "/admin/index/deletesite",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function delete_campaign(id){
	var comfirmBox;
	// alert(id);
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
		 $.ajax(
			      {
			    	 
			    	  type:"POST",
					  url: "/admin/admin/deletecampaign",
					  data: "id=" + id,
					  datatype: "html",
					  success: function(html)
					     {
						  $("#" + id).remove();
					     }
			      }
			      );
	  }
}

function edit_status(id,status){
	   var comfirmBox;
	   comfirmBox = confirm("Bạn có chắc chắn muốn thay đổi ?");
	  // alert(status);
	   if(comfirmBox == true)
		  {
		   $.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/admin/editstatus",
				  data: "id=" + id + "&status=" + status,
				  datatype: "html",
				  success: function(html)
				     {
					  
						    $("#" + id).empty();
						    $("#"+ id).prepend(html);
					    
					   // else  $("#" + id).remove();
					 }
			 });
		   
		  }
}

function loadupload(){
	var number_of_option = Number($("#count_upload").val());
	$("#count_upload").val(number_of_option + 1);
	$.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/product/uploadnew",
				  data: "count="+ (number_of_option + 1),
				  datatype: "html",
				  success: function(html)
				     {
						    $("#upload").append(html);
					 }
			 });
}

function addupload(){
	var number_of_option = Number($("#number_upload_new").val());
	$("#number_upload_new").val(number_of_option + 1);
	$.ajax(
		      {
		    	  type:"POST",
				  url: "/admin/product/addupload",
				  data: "count="+ (number_of_option + 1),
				  datatype: "html",
				  success: function(html)
				     {
						    $("#addupload").append(html);
					 }
			 });
}

function sub_upload_editproduct(id){
	$("#uploadnew_editproduct"+id).remove();
//	var number_of_option = Number($("#number_upload_new").val());
//	$("#number_upload_new").val(number_of_option - 1);
}

function sub_upload(id){
	$("#uploadnew"+id).remove();
//	var number_of_option = Number($("#count_upload").val());
//	$("#count_upload").val(number_of_option - 1);
}

function deletefile(link_file,id){
//	alert(id);
	var comfirmBox;
	 comfirmBox = confirm("Bạn có chác chắn muốn xóa không ?");
	 if(comfirmBox == true)
	  {
			$.ajax(
				      {
				    	  type:"POST",
						  url: "/admin/index/deletefile",
						  data: "link=" + link_file + "&id=" + id,
						  datatype: "html",
						  success: function(html)
						     {
							//  alert(html);
							   $("#" + id).remove();
						     }
				      }
				      );
	  }
}

/**
 * @author Namla
**/
function showForm(name){
	$("#"+name).show();
}
function check(){
	var keyword=$("#keyword").val();
	if(keyword == "" || keyword == 0){
		alert("Nội dung không chính xác");
		$("#keyword").focus;
		return false;
	}
	$("#addForm").submit;
}
function change_status(status,object_id,url){
    $.ajax({
       url: url,
       type: 'POST',
       data: 'object_id='+object_id+'&status='+status,
       success: function(data){
        if(status==0){
            $("#status"+object_id).html("OFF");
            $("a:has(#status"+object_id+")").attr("href","javascript:change_status(1,"+object_id+",'"+url+"')");
        }
        else{
            $("#status"+object_id).html("ON");
            $("a:has(#status"+object_id+")").attr("href","javascript:change_status(0,"+object_id+",'"+url+"')");
        }
       },
       error: function(data){
        alert(data);
       }
    });
    
}
function checkAll(check){
    var elements= document.getElementsByName("things[]");
    for(var i=0;i<elements.length;i++){
        elements[i].checked=check;
    } 
}

